package com.clickpaas.governmentcloud.controller.logincontroller;

import com.alibaba.fastjson.JSONObject;
import com.clickpaas.governmentcloud.common.ServerResponse;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(description = "登录接口")
@RestController
@RequestMapping(value = "/login")
public class LoginController {
	
	@ApiOperation(value = "根据jwt获取用户信息", notes = "根据jwt获取用户信息")
	@PostMapping(value = "/getUserInfoByJwt")
	public ServerResponse<?> getUserInfoByJwt(@ApiParam(value = "token") @RequestParam(value= "token", required = false) String token) {
		ServerResponse<?> serverResponse = ServerResponse.createError("获取用户信息失败");
		try {
//			token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiMTNjYWZhNi04YzM1LTRkZWMtOTMxYi1jYTc0YjI2OTdiMzAiLCJpYXQiOjE2MDQ5OTQ2MTcsInN1YiI6IntcImNvcnBOYW1lXCI6XCLliY3msr_np5HmioDmnInpmZDlhazlj7hcIixcInVuaVNjSWRcIjpcIjkxMzEwMTE0NjY2OTI2MzQxRFwiLFwicmVhbE5hbWVcIjpcInpreVwifSIsImV4cCI6MTYwNTAwMzYxN30.HN54vbD9KBj9fyQ6RnTWFt9ueYw_DuJKjrnY9kfdUKc";
			if(StringUtils.isBlank(token)) {
				return ServerResponse.createError("token不能为空");
			}
			String publicKeyStr = "8Q1E5ze+kEWPqtQaW7cVMrIzMnAIba8wj4bsUrmRmF2udcD5O2v3MfsZsd3owO2Nv/JQw0olsmMv";
			
			Claims claims = Jwts.parser()
            .setSigningKey(publicKeyStr)   
            .parseClaimsJws(token)
            .getBody();
		    //log.info("claims:【{}】",claims);
		    Object object = claims.get("jti");
		    Object sub = claims.get("sub");
		    JSONObject jsonObject = new JSONObject();
		    JSONObject subJson = JSONObject.parseObject(sub.toString());
		    String corpName = subJson.getString("corpName");
		    //log.info("用户名称：【{}】",corpName);
		    
			serverResponse = ServerResponse.createSuccess("获取成功", claims);
		} catch (Exception e) {
			serverResponse = ServerResponse.createError(403, "登录失效"); 
//			e.printStackTrace();
		}
		return serverResponse;
	}
	
	
	
	

}
